#include "widget.h"
#include "ui_widget.h"
#include <QNetworkReply>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("客户端");
    manager = new QNetworkAccessManager(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    //1，获取到输入框中的 url
    QUrl url(ui->lineEdit->text());

    //2，构造一个 HTTP 请求对象
    QNetworkRequest request(url);

    //3，发送请求
    QNetworkReply* response = manager->get(request);

    //4，通过信号槽, 来处理响应
    connect(response, &QNetworkReply::finished, this, [=]() {
        if (response->error() == QNetworkReply::NoError) // 正确获取响应
        {
            QString html = response->readAll();
            ui->plainTextEdit->setPlainText(html); //不用 QTextEdit，因为其会对 html 进行解析，就不知原始的 html 代码了
        }
        else ui->plainTextEdit->setPlainText(response->errorString());  //响应错误

        // 释放response
        response->deleteLater();
    });
}
